/**
 * 用户信息窗口
 */
var BDIphoneWindow = Window.extend({
    _ccsFileName:res.BDIphoneWindow_json,
    initWindow:function(isBD){
        var self = this;
        this.isBD=isBD;
        this._initInfo(this.isBD);
        this._addEventListener(EventKey.ScheYZM, function () {
            self.scheYZM();
        });

        this._addEventListener(EventKey.Close, function () {
            self.close();
        });
    },
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'btn_exit': '_clickExit',
            'btn_rengzheng': '_clickRenZheng'
        };
    },
    /**
     * 初始化用户数据
     * @private
     */
    _initInfo: function (isBD) {
        var self = this;

        this._nodeList['sendYZM'].setVisible(false);

        if (isBD) {//绑定手机号
            this._nodeList['change_iphone'].setVisible(false); //修改手机号 隐藏
            if (gameData.showBDcards > 0) {
                //this._nodeList['tip3'].setString('绑定手机号,首次绑定即可领取' + gameData.showBDcards + '张房卡');
            } else {
                //this._nodeList['tip3'].setString('绑定手机号。');
            }
        } else { //修改手机号
            this._nodeList['bangdingiphone'].setVisible(false);
            //this._nodeList['tip3'].setString('绑定手机号。')
        }

        var nowPos = this.getPositionY();


        var input_iphone = this._nodeList['input_iphone'];
        var input_yanzheng = this._nodeList['input_yanzheng'];
        input_iphone.setTextColor('#0D437E');
        input_yanzheng.setTextColor('#0D437E');
        input_iphone.addEventListener(function (textField, type) {
            var iphone = input_iphone.getString();
            switch (type) {
                case ccui.TextField.EVENT_ATTACH_WITH_IME:
                    if (cc.sys.os == cc.sys.OS_IOS) {
                        self.setPositionY(nowPos + 250);
                    }
                    TouchUtil.setClickDisable(this._nodeList['btn_back'] || this._nodeList['_btn_back'], true);
                    TouchUtil.setClickDisable(this._nodeList['btn_bangding'], true);
                    // cc.log("attach with IME");
                    break;
                case ccui.TextField.EVENT_DETACH_WITH_IME:
                    if (cc.sys.os == cc.sys.OS_IOS) {
                        self.setPositionY(nowPos);
                    }
                    TouchUtil.setClickDisable(this._nodeList['btn_back'] || this._nodeList['_btn_back'], false);
                    TouchUtil.setClickDisable(this._nodeList['btn_bangding'], false);
                    // cc.log("detach with IME");
                    break;
                case ccui.TextField.EVENT_INSERT_TEXT:
                    // cc.log("insert words");
                    break;
                case ccui.TextField.EVENT_DELETE_BACKWARD:
                    // cc.log("delete word");
                    break;
                default:
                    break;
            }
        }, this);

        input_yanzheng.addEventListener(function (textField, type) {
            var yanzheng = input_yanzheng.getString();
            switch (type) {
                case ccui.TextField.EVENT_ATTACH_WITH_IME:
                    if (cc.sys.os == cc.sys.OS_IOS) {
                        self.setPositionY(nowPos + 250);
                    }
                    TouchUtil.setClickDisable(this._nodeList['btn_back'] || this._nodeList['_btn_back'], true);
                    TouchUtil.setClickDisable(this._nodeList['btn_bangding'], true);

                    // cc.log("attach with IME");
                    break;
                case ccui.TextField.EVENT_DETACH_WITH_IME:
                    if (cc.sys.os == cc.sys.OS_IOS) {
                        self.setPositionY(nowPos);
                    }
                    TouchUtil.setClickDisable(this._nodeList['btn_back'] || this._nodeList['_btn_back'], false);
                    TouchUtil.setClickDisable(this._nodeList['btn_bangding'], false);
                    // cc.log("detach with IME");
                    break;
                case ccui.TextField.EVENT_INSERT_TEXT:
                    // cc.log("insert words");
                    break;
                case ccui.TextField.EVENT_DELETE_BACKWARD:
                    // cc.log("delete word");
                    break;
                default:
                    break;
            }
        }, this);

        //获取验证码
        TouchUtil.setOnclickListener(this._nodeList['btn_yanzhengma'], function (node) {
            var iphone_num = self._nodeList['input_iphone'].getString();
            var isAllNum = /^[0-9]\d*$/.test(iphone_num);

            if (iphone_num.length != 11 || !isAllNum) {
                showAlert('您输入的手机号码有误，请重新输入');
                return;
            }
            if (iphone_num === gameData.BDIphone) {
                showAlert('该手机号已绑定，请重新输入');
                return;
            }
            network.send(2008, {cmd: "VerificationCode", mobile: iphone_num});
        });
        //确认绑定
        TouchUtil.setOnclickListener(self._nodeList['btn_bangding'], function (node) {
            TouchUtil.setClickDisable(self._nodeList['btn_bangding'], true);
            setTimeout(function () {
                TouchUtil.setClickDisable(self._nodeList['btn_bangding'], false);
            }, 400);

            var iphone_num = self._nodeList['input_iphone'].getString();
            var yanzheng_num = self._nodeList['input_yanzheng'].getString();

            var isAllNum1 = /^[0-9]\d*$/.test(iphone_num);
            var isAllNum2 = /^[0-9]\d*$/.test(yanzheng_num);

            if (iphone_num.length != 11 || !isAllNum1) { //先判断手机号
                showAlert('您输入的手机号码有误，请重新输入');
                return;
            }
            if (!isAllNum2) { //再判断验证码
                showAlert('您输入的验证码有误，请重新输入');
            } else {
                //发送验证码信息
                network.send(2008, {cmd: "BindMobile", mobile: iphone_num, vi_code: yanzheng_num});
            }
        });


    },
    scheYZM: function () { //验证码发送后的倒计时
        var self = this;
        self.stopYZMSche();
        this._nodeList['btn_yanzhengma'].setVisible(false);
        this._nodeList['sendYZM'].setVisible(true);
        var num = 60;
        this.scheYZ = setInterval(function () {
            if (num <= 0) {
                self.stopYZMSche();
                self._nodeList['btn_yanzhengma'].setVisible(true);
                self._nodeList['sendYZM'].setVisible(false);
                num = 60;
            }
            --num;
            self._nodeList['sendYZM.second'].setString(num);
        }, 1000);
    },
    stopYZMSche: function () {
        var self = this;
        if (this.scheYZ) {
            clearInterval(self.scheYZ);
            self.scheYZ = null;
        }
    }
});